package com.sms.web.domain.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sms.domain.entity.Application;
import com.sms.domain.service.ApplicationService;
import com.sms.domain.service.ChannelConfigService;
import com.sms.web.rest.RestObject;

/**
 * @desc 应用控制器
 * @author wjw
 * @time 2017年11月21日下午2:22:29
 */
@Controller
@RequestMapping(value = "/admin/app")
public class ApplicationController{
    
    protected static Logger logger = LoggerFactory.getLogger(ApplicationController.class);
    
    /** application服务 */
    @Autowired
    private ApplicationService applicationService;
    
    /** application服务 */
    @Autowired
    private ChannelConfigService channelConfigService;
    
    /**
     * 配置信息列表
     * @param modelMap 模型
     * @return 视图
     */
    @RequestMapping(value = "")
    public String configList(ModelMap modelMap) {
        return "application/list";
    }
    
    /**
     * 配置信息列表分页
     * @param queryLike 查询条件
     * @param pageNumber 页数
     * @param pageSize 条数
     * @param model model
     * @return String
     */
    @RequestMapping(value = "/page")
    public String pageList(String queryLike, @RequestParam(defaultValue = "1") int pageNumber, 
    		@RequestParam(defaultValue = "20") int pageSize,
        ModelMap model) {
        Map<String, Object> filter = new HashMap<>();
        filter.put("EQ_delFlag", "0");
        if (StringUtils.isNotBlank(queryLike)) {
            filter.put("LIKE_name", queryLike);
        }
        
        int page = pageNumber < 0 ? 1 : pageNumber;
        Page<Application> list = null;
        list = applicationService.findAll(filter, page, pageSize);
        model.put("data", list);
        return "application/_page";
    }
    
    /**
     * 详细信息
     * @param id 应用id
     * @param model model
     * @return 视图
     */
    @RequestMapping(value = "/detail")
    public String userDetail(String id, ModelMap model) {
        if (StringUtils.isNotBlank(id)) {
            model.put("application", applicationService.getOne(id));
        }
        model.put("channels", channelConfigService.findAll());
        return "application/form";
    }
    
    /**
     * 保存应用
     * @param application xx
     * @return 视图
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public RestObject save(Application application) {
        Application newApp = null;
        try {
            // 如果是新增，则生成一个密钥
            if (StringUtils.isEmpty(application.getAppId())) {
                String key = UUID.randomUUID().toString().replace("-", "").substring(16);
                application.setSecretKey(key);
            }
            newApp = applicationService.save(application);
        } catch (Exception ex) {
            logger.error("在保存应用信息时异常", ex);
            return RestObject.newError(ex.getMessage());
        }
        return RestObject.newOk(newApp);
    }
    
    /**
     * 删除应用
     * @param applicationId xx
     * @return 视图
     */
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestObject del(String applicationId) {
        try {
            if (StringUtils.isNotEmpty(applicationId)) {
                applicationService.delete(applicationId);
            }
        } catch (Exception ex) {
            logger.error("在删除应用信息时异常", ex);
            return RestObject.newError(ex.getMessage());
        }
        return RestObject.newOk("success");
    }
    
}
